/**
 * 
 */
package com.music.profile.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.music.core.members.beans.Member;
import com.music.profile.constants.Gender;
import com.music.profile.dao.impl.MemberDAO;
import com.music.profile.dao.impl.MemberDAOImpl;

/**
 * @author spradeep
 *
 */
public class ProfileServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6413062022316479920L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException{
		doGet(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Map parameterMap = req.getParameterMap();
		Set keySet = parameterMap.keySet();
		for (Object object : keySet) {
			String key = (String)object;
			System.out.println(key+"="+req.getParameter(key));
		}
		MemberDAO mDao = new MemberDAOImpl();
		Date birthDate = null;
		try {
			birthDate = (new SimpleDateFormat("MM/dd/yyyy")).parse(req.getParameter("datepicker"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Long memberId = mDao.createMember(req.getParameter("firstname"), req.getParameter("lastname"), req.getParameter("password"), birthDate, Gender.valueOf(req.getParameter("gender")), req.getParameter("email"));
		System.out.println("Member Registered Successfully with member id=" + memberId);
		System.out.println("Trying to get the member");
		Member member = null;//mDao.getMember(memberId);
		System.out.println("Got the member:" + member.toString());
		resp.setContentType("text/plain");
		PrintWriter writer = resp.getWriter();
		writer.append(member.toString());
		writer.close();
	}

}
